/**
 * @param {number[][]} costs
 * @return {number}
 */
var twoCitySchedCost = function(costs) {
  costs.sort((a, b) => {
    return Math.abs(b[0] - b[1]) - Math.abs(a[0] - a[1]);
  });
  let left = 0, right = 0, result = 0;
  for(let i = 0; i < costs.length; i++) {
    if(left == costs.length / 2) {
      right++;
      result += costs[i][1];
      continue;
    } else if(right == costs.length / 2) {
      left++;
      result += costs[i][0];
      continue;
    }
    if(costs[i][0] < costs[i][1]) {
      left++;
      result += costs[i][0];
    }
    else {
      right++;
      result += costs[i][1];
    }

  }
  return result;
};
let costs = [[10,20],[30,200],[400,50],[30,20]];
console.log(twoCitySchedCost(costs));
costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]];
console.log(twoCitySchedCost(costs));
costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]];
console.log(twoCitySchedCost(costs));
